package cn.com.gridinfo.sooc;

import android.app.Activity;
import android.content.Context;

import com.jeremy.arad.Arad;
import com.jeremy.arad.AradApplication;
import com.jeremy.arad.AradApplicationConfig;
import com.jeremy.arad.utils.Log;
import com.testin.agent.TestinAgent;
import com.testin.agent.TestinAgentConfig;
import com.umeng.analytics.MobclickAgent;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.common.PublicConst;
import cn.com.gridinfo.sooc.core.my.msg.bean.PushNotice;
import cn.com.gridinfo.sooc.core.my.msg.dao.NoticeDao;

/**
 * Application
 *
 * @author jeremy
 */
public class SoocApplication extends AradApplication {
    private final String TAG = this.getClass().getName();

    //是否需要强制更新，true：是 ；false：不是
    public static int updateMust = 0;
    //检测选择日期
    private boolean tchFrag = true;
    private static SoocApplication instance;
    public static Context mContext;
    public int MAXWIDRH;
    //是否有新版本
    public static boolean hasNewVersion = false;


    public static int fromJoinPayResult = 0;//是否进入到支付结果页面 1:课程详情 2:订单
    public static boolean isPayResult = false;//是否从支付结果页面进入支付订单页，false：不是  true:是
    public static String orderId = "";//订单id

    //运用list来保存们每一个activity是关键
    private List<Activity> mList = new LinkedList<Activity>();


    protected AradApplicationConfig appConfig() {
        return new AradApplicationConfig();
    }

    @Override
    public void onCreate() {
        Log.d("应用开始运转了");
        super.onCreate();
//        实例化 application
        instance = this;
        mContext = getApplicationContext();
//        LeakCanary.install(this);
        //实例化整个应用的提示信息布局
        //实例化布局

        //开启图片调试模式
//        Arad.imageLoader.setIndicatorsEnabled(false);
        //极光推送
//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);
//        JPushInterface.setLatestNotificationNumber(this,1);
//        initData();

        //Testin统计分析
        TestinAgent.init(this, "c6420866e29c32697d7e4030cd397643", "sooc");
        TestinAgentConfig.getInstance().setDebug(PublicConst.ISUMENGEBUG);
        //U盟debug 模式
        MobclickAgent.setDebugMode(PublicConst.ISUMENGEBUG);
    }

    /**
     * 推送测试数据
     */
    private void initData() {
        for (int i = 0; i < 35; i++) {
            PushNotice notice = new PushNotice();
            notice.setNoticeDec("测试消息推送测试消息推送测试消息推送测试消息推送测试消息推送测试消息推送测试消息推送");// 设置通知显示描述内容
            notice.setNoticeTitle("测试消息推送" + i);//设置通知标题
            notice.setSuid(Arad.preferences.getString(LocalMark.SERVER_ID.getName().toString()) + "_"
                    + Arad.preferences.getString(LocalMark.USER_UID.getName().toString()));
            notice.setCreateTime(new Date(System.currentTimeMillis()));
            //存入 Android 通知数据库 状态初始化是未读 时间初始化当前时间
            NoticeDao noticeDao = new NoticeDao();
            noticeDao.saveNotice(notice);
        }
    }

    public static synchronized SoocApplication getContext() {
        return instance;
    }


    public void restartApp() {
//        Intent intent = new Intent(instance, LoginActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        instance.startActivity(intent);
//        android.os.Process.killProcess(android.os.Process.myPid());
    }


    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    //关闭每一个list内的activity
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
            MobclickAgent.onKillProcess(this);
        }
    }

    //杀进程
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
        MobclickAgent.onKillProcess(this);
    }

    public static Context getMContext() {
        return mContext;
    }
}
